import socket from "@/utils/socket";
import { defineStore } from "pinia";

export interface ISocketState {
    socket: any;
    socketMessage: string;
}

export const socketModule = defineStore('SOCKET', {
    persist: true,
    state(): ISocketState {
        return {
            socket: 'ws://192.168.31.236:9999/addtask_ws',
            socketMessage: ''
        }
    },

    actions: {
        connectSocket() {
            socket.init('ws://192.168.31.236:9999/task_ws', (message: any) => {
                let parsedData;
                try {
                    parsedData = JSON.parse(message.data);
                } catch (error) {
                    parsedData = message.data;
                }
                this.socketMessage = parsedData
            })
            this.socket = socket
        },

        closeSocket() {
            this.socket.close();
        }
    }
})